#include "stdafx.h"
#include "PlayerPaddle.h"
#include "Game.h"
#include "ServiceLocator.h"
#include "IInputProvider.h"

PlayerPaddle::PlayerPaddle() :
_velocity(0),
_maxVelocity(1200.0f)
{
	Load("images/paddle.png");
	assert(IsLoaded());
	_width = GetSprite().getTextureRect().width;
	_height = GetSprite().getTextureRect().height;

	GetSprite().setOrigin(_width / 2, _height / 2);
}


PlayerPaddle::~PlayerPaddle()
{
}

void PlayerPaddle::Draw(sf::RenderWindow& window)
{
	VisibleGameObject::Draw(window);
}

float PlayerPaddle::GetVelocity() const
{
	return _velocity;
}

void PlayerPaddle::Update(float elapsedTime)
{
	int xMax = Game::SCREEN_WIDTH;
	int yMax = Game::SCREEN_HEIGHT;
	if(ServiceLocator::GetInput()->KeyPressed(KeyActions::MoveLeft))
	{
		_velocity -= 1.0f;
	}
	else if (ServiceLocator::GetInput()->KeyPressed(KeyActions::MoveRight))
	{
		_velocity += 1.0f;
	}
	else if (ServiceLocator::GetInput()->KeyPressed(KeyActions::MoveDown))
	{
		_velocity = 0.0f;
	}
	else
	{
		if (_velocity > 0)
			_velocity -= 3.0f;
		if (_velocity < 0)
			_velocity += 3.0f;
	}
	
	if (_velocity > _maxVelocity)
		_velocity = _maxVelocity;

	else if (_velocity < -_maxVelocity)
		_velocity = -_maxVelocity;

	sf::Vector2f pos = this->GetPosition();

	if (pos.x < _width / 2)
	{
		pos.x = _width / 2;
		GetSprite().setPosition(pos);
		_velocity = -_velocity / 2;
	}
	if (pos.x >(xMax - _width / 2))
	{
		pos.x = xMax - _width / 2;
		GetSprite().setPosition(pos);
		_velocity = -_velocity / 2;
	}
	GetSprite().move(_velocity * elapsedTime, 0);
}

